const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}


function getWeather(callback){
  wx.getLocation({
    success: function (res) {
      let jd = res.longitude;
      let wd = res.latitude;
      let ak = "blzakbjDYiicLx8VgGORroD2hKWwPIRI";
      let url = "https://api.map.baidu.com/telematics/v3/weather?location=" + jd + "," + wd + "&output=json&ak=" + ak;

      wx.request({
        url: url,
        dataType: 'json',
        success: function (res) {
          if (res.statusCode == 200) {
            callback(res.data);
          }
        }
      })

    },
  })
}

module.exports = {
  formatTime: formatTime,
  getWeather: getWeather
}
